package com.haizhi.crm.api.controller.sys.auth;

import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.sys.auth.service.RelationPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by yangyijun on 2018/5/22.
 */
@Api(description = "[系统-权限-关系权限]-增删改查" )
@RestController
@RequestMapping("/relationPermission" )
public class RelationPermissionController {

    @Autowired
    private RelationPermissionService relationPermissionService;

    @PostMapping("/save" )
    @ApiOperation(value = "设置-关系权限管理-添加关系权限", notes = "excel字段顺序：\"userNo\", \"entityKey\", \"startDt\", \"endDt\"" )
    public Response save(@RequestParam(value = "file", required = false) MultipartFile file,
                         @ApiParam(value = "关系ID", required = true) @RequestParam Long relationId,
                         @ApiParam(value = "资源ID数组", required = true) @RequestParam Long[] resourceIds) {
        return relationPermissionService.save(file, relationId, resourceIds);

    }

    @PostMapping("/update" )
    @ApiOperation(value = "设置-关系权限管理-修改关系权限" )
    public Response update(@RequestParam(value = "file", required = false) MultipartFile file,
                           @ApiParam(value = "关系ID", required = true) @RequestParam Long relationId,
                           @ApiParam(value = "资源ID数组", required = true) @RequestParam Long[] resourceIds) {
        return relationPermissionService.update(file, relationId, resourceIds);
    }

    @DeleteMapping("/delete" )
    @ApiOperation(value = "设置-关系权限管理-删除关系权限" )
    public Response delete(@RequestParam Long relationId) {
        return relationPermissionService.delete(relationId);
    }

    @GetMapping("/list" )
    @ApiOperation(value = "设置-关系权限管理-查看关系权限列表" )
    public Response list() {
        return relationPermissionService.list();
    }

    @GetMapping("/detail" )
    @ApiOperation(value = "设置-关系权限管理-查看关系权限信息", notes = "可以用在详情页或者修改页面获取关系的详细权限信息" )
    public Response detail(@RequestParam Long relationId) {
        return relationPermissionService.detail(relationId);
    }

    @GetMapping("/download" )
    @ApiOperation(value = "设置-关系权限管理-下周关系明细excel文件" )
    public void download(HttpServletResponse response, @RequestParam String fileRelativePath) {
        relationPermissionService.download(response, fileRelativePath);
    }
}
